package org.nf.admin.api;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.nf.product.model.Product;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;
import util.HttpClientUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 产品查询
 */
public class ProductController extends BaseController{

//    @RequestMapping("/doWebList")
//    public View doGet(HttpSession session) {
//        Map<String, String> map = new HashMap<>();
//        map.put("name", "user1");
//        String result = HttpClientUtils.sendGet("http://localhost:8080/listProduct", map);
//
//        Object products = session.getAttribute("products");
//        return new JsonView(result);
//    }

    /**
     * 查询全部
     * @param product
     * @return
     */
//    @RequestMapping("/doListProduct")
//    public View listProduct(String product){
//        Map<String, String> map = new HashMap<>();
//        map.put("product", product);
//        String result = HttpClientUtils.sendGet("http://localhost:8080/listProduct", map);
//        return new JsonView(result);
//    }

    @RequestMapping("/doListProduct")
    public View listProduct(String product){
        Map<String, String> map = new HashMap<>();
        map.put("product", product);
        String result = HttpClientUtils.sendGet("http://localhost:8080/listProduct", map);
        //使用gson将json字符串反序列化
        Gson gson = new Gson();
        TypeToken<Map<String, Object>> token = new TypeToken<Map<String, Object>>() {};
        Map<String, Object> responseMap = gson.fromJson(result, token.getType());

        // 输出反序列化后的Map对象
        System.out.println(responseMap);

        // 或者将反序列化后的Map对象转换为JSON字符串再输出
        String jsonOutput = gson.toJson(responseMap);
        System.out.println(jsonOutput);
        return new JsonView(result);
    }

    /**
     * 查询名称
     * @param productName
     * @return
     */
    @RequestMapping("/getProductFunction")
    public View getProduct (String productName){
        Map<String, String> map = new HashMap<>();
        map.put("productName", productName);
        String result = HttpClientUtils.sendGet("http://localhost:8080/getProduct", map);
        return new JsonView(result);
    }

    /**
     * 查询分页
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/doListProductPaging")
    public View listProductPaging (String page,String limit){
        Map<String, String> map = new HashMap<>();
        map.put("productName", page);
        map.put("limit", limit);
        String result = HttpClientUtils.sendGet("http://localhost:8080/listProductPaging", map);
        return new JsonView(result);
    }

}
